/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package com.xujl.hsns.server;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xujl.hsns.carriers.Constants;
import com.xujl.hsns.carriers.Invoke;
import com.xujl.hsns.carriers.Result;




/**
 * @author jinliang.xu
 *  Class the extends IoHandlerAdapter in order to properly handle
 *  connections and the data the connections send
 */
public class HSNSServerHandler extends IoHandlerAdapter {

	private final static Logger LOGGER = LoggerFactory.getLogger(HSNSServer.class);
	
	ServicesClip clip;
	

    public HSNSServerHandler(ServicesClip clip) {
        this.clip=clip;
    }

    
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        cause.printStackTrace();
        
    }

    
    public void messageReceived(IoSession session, Object message)
            throws Exception {
    	Invoke invokePacking = (Invoke)message;// codec.decode(message,				InvokePacking.class);
    	LOGGER.debug(invokePacking.getM());
    	LOGGER.debug(message.toString());
		Result result= new Result();
		result.s=Constants.INVOKE_SATE_OK;
		try {
			if("?".equalsIgnoreCase(invokePacking.getM())){
				result.v=clip.getServices();
			}else if("/".equalsIgnoreCase(invokePacking.getM())){
				result.v=clip.getServiceMethods();
			}else{
				result.v=clip.invoke(invokePacking.getM(), invokePacking.getA());
			}
		} catch (Throwable e) {
			result.ex=e;
			result.s=Constants.INVOKE_SATE_EX;
		}	
		LOGGER.debug("result:" + result);
		session.write(result);
		LOGGER.debug(invokePacking.getM()+"ok");
		//session.close(true);
	}

    
    public void sessionClosed(IoSession session) throws Exception {
      //  System.out.println("Session closed...");

    }

    
    public void sessionCreated(IoSession session) throws Exception {

       // System.out.println("Session created...");

    }

    
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
      //  System.out.println("Session idle...");
    }

    
    public void sessionOpened(IoSession session) throws Exception {
      //  System.out.println("Session Opened...");
    }
}
